var app = getApp();
Page({
	data: {
		show: true,
		img_url: 'http://17fx.com/imgs/',
    list:null,

    order_status_list: null,
    tab_index: 0,
    slider_offset: 0,
    slider_left: 0,
    slider_width: 0,
    order_list: [],
    page:1
	},
  //点击订单状态刷新 0全部1待付款2待确定3已完成
  handleTab: function (event) {
    var that = this
    that.setData({
      slider_offset: event.currentTarget.offsetLeft,
      tab_index: event.currentTarget.id,
      page:1
    });
    console.log(event.currentTarget.id)
    console.log('订单')
    //调用接口
    Getglist(that, getApp().globalData.openid,that.data.tab_index,1)
  },
	onLoad: function () {
    this.setData({
      slider_left: 0,
      slider_offset: this.data.window_width / 2 * this.data.tab_index,
      slider_width: this.data.window_width / 2
    });
    // 生命周期函数--监听页面加载
    var that = this
    //创建一个dialog
    wx.showToast({
      title: '正在加载中...',
      icon: 'loading',
      duration: 10000
    });
    Getglist(that, getApp().globalData.openid,0,1)
    
	},        
	onShow: function () {
    var that = this
	},
	onScroll: function (e) {
	},
	tapSearch: function () {
	},

  //订单详情
  dingdanxq: function (event) {
    var order_id = event.currentTarget.dataset.orid
    wx.navigateTo({
      url: 'articleOrder?id='+order_id,
      success: function (res) {
        // success
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  //查看商品详情
  chakanxqing: function (event){
    wx.navigateTo({
      url: '/pages/detail/shangping?id=' + event.currentTarget.dataset.gid,
      success: function (res) {
        // success
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },


  //立即付款
  ljfukuan:function (event){
    var that = this;
    var order_id = event.currentTarget.dataset.oid
    wx.request({
      url: 'https://17fx.com/weapp/api/order_pay.html',
      data: {
        openid: getApp().globalData.openid,
        order_id: order_id
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)

        //调用支付
        GetList3(that, getApp().globalData.openid, res.data.goods_num, res.data.goods_price, res.data.goods_id,order_id)
      }
    });

  },
	tapShow: function () {
		var isShow = this.data.show;
		this.setData({
			show: !isShow
		})
	},

  //上拉加载数据绑定
  onReachBottom: function () {
    var that = this
    //调用getlist方法加载排行集合
    Getglist(that, getApp().globalData.openid, that.data.tab_index,that.data.page)
  },
  //下拉刷新
  onPullDownRefresh: function () {
    console.log("下拉刷新数据绑定")
    var that = this
    //清空集合
    that.setData({
      list: [],
      page:1
    })
    Getglist(that, getApp().globalData.openid, that.data.tab_index,1)

  }




});

var url = "https://17fx.com/weapp/api/getorder.html";
//
var Getglist = function (that, openid,tab_index,page) {
  wx.request({
    url: url,
    data: {
      openid: openid,
      tab_index:tab_index,
      page:page,
      page_size:10
    },
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      
      console.log(res.data);
      if (page == 1) {
        that.setData({
          list: []
        })
      }
      if (res.data != null) {
        var list = that.data.list;
        for (var i = 0; i < res.data.length; i++) {
          list.push(res.data[i]);
        }
        that.setData({
          list: list,
          page: that.data.page + 1
        })
      }
      wx.hideToast();
      if(res.data.length == 0){
        //创建一个dialog
        wx.showToast({
          title: '没有加载数据了',
          icon: 'loading',
          duration: 1500
        });
      }
      
      


    }
  })
}



var url3 = "https://17fx.com/weapp/api/getOrderPayment.html";
//  调取微信支付
var GetList3 = function (that, openid, number, shop_prices, goods_id,order_id) {
  wx.request({
    url: url3,
    data: {
      openid: openid,
      number: number,
      shop_prices: shop_prices,
      goods_id: goods_id,
      buy_leader: getApp().globalData.suid,
      order_id:order_id
    },
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      // success
      console.log(res.data);
      console.log("调用支付123");
      that.setData({

        //prepay_id:res.data['prepay_id'],
        //token:res.data['token'],
        //uopenid:res.data['uopenid'],
        //packages:res.data['package'],
        //time:res.data['time']
      })

      //弹出提示，库存不足，每人每个商品超出购买数量
      if (res.data == 0) {
        wx.showModal({
          title: '提示',
          content: '已售完，请关注17分享商城公众号获取执行抢购优惠',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
      if (res.data == 1) {
        wx.showModal({
          title: '提示',
          content: '一次只能购买一份',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
      if (res.data == 2) {
        wx.showModal({
          title: '提示',
          content: '只能购买一份',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
      if (res.data == 3) {
        wx.showModal({
          title: '提示',
          content: '暂未开始',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
      //支付
      wx.requestPayment({
        'timeStamp': res.data['timeStamp'],
        'nonceStr': res.data['nonceStr'],
        'package': res.data['package'],
        'signType': 'MD5',
        'paySign': res.data['paySign'],
        'success': function (res) {
          console.log("zhifu成功");
          console.log(res);
          console.log(res.data);
          //跳转到订单页面（导航页）
          wx.switchTab({
            url: '/pages/order/order'
          })




          //发送模板消息
          // var fId = that.data.prepay_id;
          // console.log(that.data.prepay_id);
          // var l = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + that.data.token;
          // var d = {  
          // "touser": that.data.uopenid,
          // "template_id": 'XL4mtVOlGtG7GN7gZAxZbvHDDNk6HgR_rcxkThuHmcU',//这个是1、申请的模板消息id，  
          // "page": '/pages/index/index',  
          // "form_id": fId,  
          // "data": {//测试完发现竟然value或者data都能成功收到模板消息发送成功通知，下面的keyword*是你1、设置的模板消息的关键词变量  

          //     "keyword1": {  
          //     "value": "探陌爱心",  
          //     "color": "#4a4a4a"  
          //     },  
          //     "keyword2": {  
          //     "value": "爱心点赞",  
          //     "color": "#9b9b9b"  
          //     },  
          //     "keyword3": {  
          //     "value": that.data.time,  
          //     "color": "#9b9b9b"  
          //     }
          // },  
          // color: '#ccc',  

          // }  
          // wx.request({
          //     url: l,
          //     data: d,
          //     method: 'POST',
          //     success: function(res){
          //         console.log("push msg");
          //         console.log(res);
          //     },
          //     fail: function(err) {
          //         // fail
          //         console.log("push err")
          //         console.log(err);
          //     }
          //     });




        },
        'fail': function (res) {
          console.log("zhifu失败");
          console.log(res);
        }
      })
    },
    fail: function () {
      // fail
      console.log("调用微信支付接口出错");
    },
    complete: function () {
      // complete
    }
  })
}